package org.liaohailong.victorlibrary.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import org.liaohailong.library.widget.interact.Message
import org.liaohailong.library.widget.interact.TVInteractDialogFragment
import org.liaohailong.victorlibrary.R
import org.liaohailong.victorlibrary.base.BaseActivity
import java.util.*

/**
 * Author: liaohailong
 * Date: 2019/3/11
 * Time: 5:39 PM
 * Description: TV互动插件测试UI
 **/
class TVInteractActivity : BaseActivity() {
    companion object {
        private const val TEST_IMAGE_URL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1552366847035&di=8d5a90a2935cba1e094bb8f3a9826fac&imgtype=0&src=http%3A%2F%2Fwww.08lr.cn%2Fuploads%2Fallimg%2F170426%2F1-1F426193550.jpg"
        private const val TEST_AUDIO_URL = "https://dev-cdn.api.wanzi.cn/debug/test/3.mp4"

        fun show(context: Context) {
            val intent = Intent(context, TVInteractActivity::class.java)
            context.startActivity(intent)
        }
    }

    private var tvInteractDialogFragment: TVInteractDialogFragment? = null

    private val testData: LinkedList<Message> = LinkedList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_tv_interact)

        initTVInteract()
    }

    private fun initTVInteract() {
        window.decorView.postDelayed({
            val random = Random()
            testData.clear()
            for (i in 1 until 10) {
                when (random.nextInt(3)) {
                    0 -> {
                        addText(i)
                    }
                    1 -> {
                        addImage(i)
                    }
                    2 -> {
                        addAudio(i)
                    }
                }
            }

            //模拟发送
            val timer = Timer()
            timer.schedule(object : TimerTask() {
                override fun run() {
                    runOnUiThread {
                        val pollFirst = testData.pollFirst()
                        if (pollFirst != null) showTVInteractMessage(pollFirst)
                        else {
                            cancel()
                            initTVInteract()
                        }
                    }
                }
            }, 0, 1000)
        }, 2000)

    }

    private fun showTVInteractMessage(message: Message) {
        if (tvInteractDialogFragment == null) {
            tvInteractDialogFragment = TVInteractDialogFragment().apply {
                show(
                        supportFragmentManager,
                        TVInteractDialogFragment::class.java.canonicalName,
                        message,
                        object : TVInteractDialogFragment.OnTVInteractDialogCallback {
                            override fun onTVInteractDialogDismiss(dialog: TVInteractDialogFragment) {
                                tvInteractDialogFragment = null
                            }
                        })
            }
        } else tvInteractDialogFragment!!.sendMessage(message)
    }


    private fun addText(index: Int) {
        val title = "第" + index + "号数据---文本类型"
        testData.add(Message(
                title,
                title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title + title,
                "",
                Message.Type.TEXT))
    }

    private fun addImage(index: Int) {
        val title = "第" + index + "号数据---图片类型"
        testData.add(Message(
                title,
                "",
                TEST_IMAGE_URL,
                Message.Type.IMAGE))
    }

    private fun addAudio(index: Int) {
        val title = "第" + index + "号数据---音频类型"
        testData.add(Message(
                title,
                "",
                TEST_AUDIO_URL,
                Message.Type.AUDIO))
    }
}